package com.wisdudu.blank.ui.login;

import android.databinding.ObservableField;
import android.os.CountDownTimer;
import android.text.TextUtils;

import com.kelin.mvvmlight.base.ViewModel;
import com.kelin.mvvmlight.command.ReplyCommand;
import com.wisdudu.blank.data.Injection;
import com.wisdudu.blank.data.repo.UserRepo;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.Abs;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.ProgressSubscriber;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextErrorListener;
import com.wisdudu.blank.data.source.remote.client.retrofit.subscribers.SubscriberOnNextListener;
import com.wisdudu.blank.databinding.FragmentForgetPassBinding;
import com.wisdudu.blank.support.util.ToastUtil;
import com.wisdudu.blank.support.widget.loading.LoadingProgressDialog;
import com.wisdudu.frame.support.util.RegularUtil;

/**
 * Created by ypp on 2016/8/23.
 */
public class ForgetPasswordVM implements ViewModel {
    private static final String TAG = "RegisterVM";
    private FragmentForgetPassBinding mBingding;
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<String> rePassword = new ObservableField<>();
    public ObservableField<String> code = new ObservableField<>();
    public ObservableField<Integer> step1 = new ObservableField<>();
    public ObservableField<Integer> step2 = new ObservableField<>();
    private ForgetPasswordFragment mFragment;
    private UserRepo mUserRepo;
    public VerifyCodeReTimer mVerifyCodeReTimer;
    private String serviceCode = "0";

    public ForgetPasswordVM(ForgetPasswordFragment fragment, FragmentForgetPassBinding binding) {
        this.mFragment = fragment;
        this.mBingding = binding;
        mUserRepo = Injection.provideUserRepo();
        step1.set(1);
        step2.set(0);
    }


    public ReplyCommand getCodeOnClick = new ReplyCommand(() -> {
        getSmsCode();
    });

    public ReplyCommand step1OnClick = new ReplyCommand(() -> {
        if (TextUtils.isEmpty(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码不能为空");
            return;
        }
        if (!RegularUtil.isMobile(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码格式错误");
            return;
        }
        if (TextUtils.isEmpty(code.get())) {
            ToastUtil.INSTANCE.toast("验证码不能为空");
            return;
        }
        int sCode = (int) Double.parseDouble(serviceCode);
        if (sCode != Integer.parseInt(code.get())) {
            ToastUtil.INSTANCE.toast("验证码错误,请重新输入");
            return;
        }
        step1.set(0);
        step2.set(1);
    });

    public ReplyCommand confirmOnClick = new ReplyCommand(() -> {
        confirm();
    });

    private void confirm() {
        if (TextUtils.isEmpty(password.get())) {
            ToastUtil.INSTANCE.toast("密码不能为空");
            return;
        }
        if (!RegularUtil.isPassword(password.get())) {
            ToastUtil.INSTANCE.toast("请输入6-20位密码");
            return;
        }
        if (TextUtils.isEmpty(rePassword.get())) {
            ToastUtil.INSTANCE.toast("重复密码不能为空");
            return;
        }
        if (!rePassword.get().equals(password.get())) {
            ToastUtil.INSTANCE.toast("两次密码输入不一致");
            return;
        }
        mUserRepo.resetPwd(new ProgressSubscriber<Abs>(new SubscriberOnNextListener<Abs>() {
            @Override
            public void onNext(Abs abs, LoadingProgressDialog pd) {
                pd.setTitle("修改成功，请重新登录");
                mFragment.removeFragment();
            }
        }, mFragment.getActivity(), "正在修改...", 1000), phone.get(), code.get(), rePassword.get());
    }

    private void getSmsCode() {
        if (TextUtils.isEmpty(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码不能为空");
            return;
        }
        if (!RegularUtil.isMobile(phone.get())) {
            ToastUtil.INSTANCE.toast("手机号码格式错误");
            return;
        }
        mUserRepo.getSmsCode(phone.get(), 2)
                .subscribe(new ProgressSubscriber<>(new SubscriberOnNextErrorListener<Object>() {

                    @Override
                    public void onError(Throwable e, LoadingProgressDialog pd) {
                        ToastUtil.INSTANCE.toast(e.getMessage());
                    }

                    @Override
                    public void onNext(Object abs, LoadingProgressDialog pd) {
                        pd.setTitle("发送成功");
                        serviceCode = String.valueOf(abs);
                        mVerifyCodeReTimer = new VerifyCodeReTimer(30000, 1000);
                        mVerifyCodeReTimer.start();
                    }
                }, mFragment.getActivity(), "正在发送..."));
    }

    private class VerifyCodeReTimer extends CountDownTimer {

        public VerifyCodeReTimer(long millisUntilFinished, long countDownInterval) {
            super(millisUntilFinished, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mBingding.btnGetCode.isClickable()) {
                mBingding.btnGetCode.setClickable(false);
            }
            mBingding.btnGetCode.setText("重新发送(" + String.valueOf(millisUntilFinished / 1000) + ")");
        }

        @Override
        public void onFinish() {
            if (!mBingding.btnGetCode.isClickable()) {
                mBingding.btnGetCode.setClickable(true);
            }
            mBingding.btnGetCode.setText("重新发送");
        }
    }

    public void setTimeCancel() {
        if (mVerifyCodeReTimer != null) {
            mVerifyCodeReTimer.cancel();
        }
    }

}
